package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DstoreInExcution;
import com.fortune.dto.in.dStoreIn.DstoreInListDto;
import com.fortune.dto.in.dStoreIn.DstoreInitAddDto;
import com.fortune.dto.in.dStoreIn.DstoreInitEditDto;
import com.fortune.dto.in.dealerWarehouseReceiptWarehouse.ConfirmStorage;
import com.fortune.dto.in.dealerWarehouseReceiptWarehouse.DealerWarehouseReceiptWarehouseDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DstoreInEnum;
import com.fortune.service.DstoreInService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.dStorage.DstorageInfo;
import com.fortune.vo.dStoreIn.DinitDetails;
import com.fortune.vo.dStoreIn.DstoreInInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/5/26 0026.
 */
@Api(value = "storeIn", description = "经销商仓库初始化", tags = {"经销商仓库初始化"})
@RestController
@RequestMapping("storeIn")
public class DstoreInController {
    @Autowired
    private DstoreInService dstoreInService;

    @ApiOperation(value = "经销商仓库初始化列表", nickname = "dStoreList", notes = "", tags={ "@安童" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData> list(HttpServletRequest request, @ApiIgnore @Validated DstoreInListDto dstoreInListDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        dstoreInListDto.setPage((dstoreInListDto.getPage()-1)*10);
        List<DstoreInInfo> list = dstoreInService.findStoreList(dstoreInListDto,mid,did);
        int count = dstoreInService.findStoreListCount(mid,did);
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DstoreInExcution<PagingData>(pagingData, DstoreInEnum.FIND_SUCCESS);
    }

    @ApiOperation(value = "添加初始化单", nickname = "dStoreInAdd", notes = "", tags={ "@安童" })
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DstoreInitAddDto addDto) {
       HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        String id = UUIDUtil.get32UUID();
        int num = dstoreInService.queryDInitInfoIsExist(mid,did,addDto.getNum(),addDto.getStoreId());
        if (num > 1){
            return new DstoreInExcution<Void>(null, DstoreInEnum.EXIST);
        }
        num = dstoreInService.addDInitInfo(mid,did,id,addDto);
        if (num < 1){
            return new DstoreInExcution<Void>(null, DstoreInEnum.FAILURE);
        }
        return new DstoreInExcution<Void>(null, DstoreInEnum.SUCCESS);
    }

    @ApiOperation(value = "编辑初始化单", nickname = "dStoreInEdit", notes = "", tags={ "@安童" })
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DstoreInitEditDto editDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        int num = dstoreInService.queryDInitInfoIsExist(mid,did,editDto.getNum(),editDto.getStoreId());
        if (num > 1){
            return new DstoreInExcution<Void>(null, DstoreInEnum.EXIST);
        }
        num = dstoreInService.editDInitInfo(mid,did,editDto);
        if (num < 1){
            return new DstoreInExcution<Void>(null, DstoreInEnum.FAILURE);
        }
        return new DstoreInExcution<Void>(null, DstoreInEnum.SUCCESS);
    }

    @ApiOperation(value = "删除初始化单", nickname = "dStoreInDelete", notes = "", tags={ "@安童" })
    @ApiImplicitParam(name="id", value="初始化单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="初始化单id不能为空")String id) {
      HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        int num = dstoreInService.deleteDInitInfo(mid,did,id);
        if (num < 1){
            return new DstoreInExcution<Void>(null, DstoreInEnum.FAILURE);
        }
        return new DstoreInExcution<Void>(null, DstoreInEnum.SUCCESS);
    }

    @ApiOperation(value = "初始化单详情", nickname = "dStoreInInfo", notes = "", tags={ "@安童" })
    @ApiImplicitParam(name="id", value="初始化单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "info",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<DinitDetails> info(HttpServletRequest request, @Validated @NotBlank(message="初始化单id不能为空")String id) {
      HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        DinitDetails dinitDetails = dstoreInService.queryStoreInitDetails(mid,did,id);
        return new DstoreInExcution<DinitDetails>(dinitDetails, DstoreInEnum.SUCCESS);
    }

    @ApiOperation(value = "确认初始化单", nickname = "dStoreInitConfirm", notes = "", tags={ "@安童" })
    @ApiImplicitParam(name="id", value="初始化单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "confirm",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> confirm(HttpServletRequest request,@Validated @NotBlank(message="初始化单id不能为空")String id) {
      HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        int num = dstoreInService.storeInitConfirm(mid,did,id);
        if (num <1){
            return new DstoreInExcution<Void>(null, DstoreInEnum.FAILURE);
        }
        return new DstoreInExcution<Void>(null, DstoreInEnum.SUCCESS);
    }


    @ApiOperation(value = "经销商仓库收货入库", nickname = "dealerWarehouseReceiptWarehouse", notes = "", tags={ "@安童" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="search", value="搜索",paramType="query",dataType="String"),
            @ApiImplicitParam(name="storageStatus", value="搜索",paramType="query",dataType="int")
    })
    @PutMapping(value = "storageList",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData> storageList(HttpServletRequest request, @ApiIgnore @Validated DealerWarehouseReceiptWarehouseDto dealerDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        dealerDto.setPage((dealerDto.getPage()-1)*10);
        List<DstorageInfo> list = dstoreInService.findStorageList(dealerDto,mid,did);
        int count = dstoreInService.findStorageListCount(dealerDto,mid,did);
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DstoreInExcution<PagingData>(pagingData, DstoreInEnum.FIND_SUCCESS);
    }

    @ApiOperation(value = "经销商确认入库", nickname = "confirmStorage", notes = "", tags={ "@安童" })
    @PutMapping(value = "confirmStorage",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> confirmStorage(HttpServletRequest request,@Validated @RequestBody ConfirmStorage confirmStorage) {
       HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String mid = dUserInfo.getmId();
        String did = dUserInfo.getdId();
        String chickId = dUserInfo.getId();
        int num = dstoreInService.confirmStorage(confirmStorage,mid,did,chickId);
        if(num > 0){
            return new DstoreInExcution<Void>(null, DstoreInEnum.SUCCESS);
        }else{
            return new DstoreInExcution<Void>(null, DstoreInEnum.FAILURE);
        }
    }
}
